<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

class mobileapi_rpc_salesgoods extends mobileapi_rpc_goods
{
    /**
     * 构造方法
     * @param object application
     */
    public function __construct(&$app)
    {
        $this->app = $app;
    }

    public function sales_list($filter)
    {
            $cat_id = $filter['cat_id'];
            if($filter['virtual_cat_id']){
        	$goodsVcatModel = kernel::single('b2c_mdl_goods_virtual_cat');
        	$vcat = $goodsVcatModel->dump($filter['virtual_cat_id']);
        	
        	if(!$vcat)return array('total_results'=>0, 'items'=>'[]');
        		
        	parse_str($vcat['filter'],$filter_vartual);
        	$filter_vartual['marketable'] = 'true';
        	$filter_vartual['is_buildexcerpts'] = 'true';
                $filter_vartual['store|than'] = 0;
            }
                $filter['status'] = 'true';
                $filter['rule_id'] = $filter['rule_id'];
                $filter['from_time|lthan'] = time();
                $filter['to_time|than'] = time();
        
        $arr = $this->app->model('sales_goods')->getList( '*',$filter);
        if(is_array($arr) && $arr[0]['conditions']['goods_select'] != ''){
          foreach ($arr as $k => $v) {
          
              $arr[$k]['goods'] = $this->get_sales_goods(array('ids'=>$v['conditions']['goods_select'],'description'=>'false'),$cat_id,$filter_vartual); 
              $arr[$k]['total'] = count($arr[$k]['goods']);
          }
            
        }
		$arr['system_time']=(string)time();

        return $arr;
    }

    public function get_sales_goods($params = array(),$cat_id=0,$filter_vartual=array())
    {
        $sids = $params['ids'];
        if(!$sids) return;
        $filter_vartual['goods_id|in'] = explode(',', $sids);
        $rows = app::get('b2c')->model('goods')->getList('*',$filter_vartual);
        $sdf_goods = array();
        foreach($rows as $k=>$arr_row){
 
            $sdf_goods[$k] = kernel::single('mobileapi_rpc_goods')->get_item_detail($arr_row, '');
            $sdf_goods[$k]['cat_id'] =$arr_row['cat_id'];
			if(isset($params['description'])&&$params['description']=='false'){
				unset($sdf_goods[$k]['description']);
				unset($sdf_goods[$k]['wapintro']);
				
			}
        }
        $sids=explode(',',$sids);
        $temArr=array();
        foreach ($sids as $k2 => $v2){

            foreach($sdf_goods as $k1=>$v1){
              if($v2==$v1['iid']){
                $temArr[]=$v1;
                unset($sdf_goods[$k1]);

              }
              if(!$v1['marketable']){
                  unset($sdf_goods[$k1]);
              }
            }
        }
        $resArr=array_merge($sdf_goods,$temArr); 
        if ($cat_id>0) {
            $cat_ids = app::get("b2c")->model("goods_cat")->getList("cat_id",array('parent_id'=>$cat_id));
            $c_ids = array($cat_id);
            foreach($cat_ids as $c_id){
                $c_ids[] = $c_id['cat_id'];
            }
         foreach ($resArr as  $vc) {
          if (in_array($vc['cat_id'],$c_ids)) {
             $resNarr[] = $vc;

          }
          
         }
         return $resNarr;
        }

        return $resArr;
    }

    function get_sales_ads($params = array()) {
  //       $ad_type = $params['ad_type'] ? $params['ad_type'] : 'common';
  //       $ad_type_id = $params['ad_type_id'];

  //       $db = kernel::database();

  // $sql = "select *  FROM `sdb_mobileapi_sales_ads` where disabled = 'false' and ad_type = '".$ad_type."' and ad_type_id = '".$ad_type_id."' order by ordernum asc";
  // $indexads = $db->select($sql);

     $aa=$this->app->model('sales_ads')->getList('*');
  
       
      
        $groupad = array();
        foreach ($aa as $key => $v){
            $v['ad_img'] = base_storager::image_path($v['ad_img']);
            $groupad[$v['group_code']]['items'][] = $v;
        }
        
        $res = array();
        foreach ($groupad as $key => $value) {
            $res[] = $value;
        }
        
        return $res;
        
        //print_r($res);exit;
    }
    

}
